/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.wise.common.device.type.service.impl;

import com.wise.common.device.type.domain.GfDeviceType;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.wise.common.device.type.repository.GfDeviceTypeRepository;
import com.wise.common.device.type.service.GfDeviceTypeService;
import com.wise.common.device.type.service.dto.GfDeviceTypeDto;
import com.wise.common.device.type.service.dto.GfDeviceTypeQueryCriteria;
import com.wise.common.device.type.service.mapstruct.GfDeviceTypeMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author gaocm
* @date 2020-10-30
**/
@Service
@RequiredArgsConstructor
public class GfDeviceTypeServiceImpl implements GfDeviceTypeService {

    private final GfDeviceTypeRepository gfDeviceTypeRepository;
    private final GfDeviceTypeMapper gfDeviceTypeMapper;

    @Override
    public Map<String,Object> queryAll(GfDeviceTypeQueryCriteria criteria, Pageable pageable){
        Page<GfDeviceType> page = gfDeviceTypeRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(gfDeviceTypeMapper::toDto));
    }

    @Override
    public List<GfDeviceTypeDto> queryAll(GfDeviceTypeQueryCriteria criteria){
        return gfDeviceTypeMapper.toDto(gfDeviceTypeRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public GfDeviceTypeDto findById(Long id) {
        GfDeviceType gfDeviceType = gfDeviceTypeRepository.findById(id).orElseGet(GfDeviceType::new);
        ValidationUtil.isNull(gfDeviceType.getId(),"GfDeviceType","id",id);
        return gfDeviceTypeMapper.toDto(gfDeviceType);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public GfDeviceTypeDto create(GfDeviceType resources) {
        return gfDeviceTypeMapper.toDto(gfDeviceTypeRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(GfDeviceType resources) {
        GfDeviceType gfDeviceType = gfDeviceTypeRepository.findById(resources.getId()).orElseGet(GfDeviceType::new);
        ValidationUtil.isNull( gfDeviceType.getId(),"GfDeviceType","id",resources.getId());
        gfDeviceType.copy(resources);
        gfDeviceTypeRepository.save(gfDeviceType);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            gfDeviceTypeRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<GfDeviceTypeDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (GfDeviceTypeDto gfDeviceType : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("设备型号", gfDeviceType.getDeviceUnit());
            map.put("设备名称", gfDeviceType.getDeviceName());
            map.put("主轴数量", gfDeviceType.getDeviceArc());
            map.put("设备类型编号", gfDeviceType.getDeviceTypeCode());
            map.put("数控系统类型", gfDeviceType.getSystemType());
            map.put("设备种类", gfDeviceType.getDeviceKind());
            map.put("父id", gfDeviceType.getParentId());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}